var mysql = require('mysql');
var express = require('express');
var crypto = require('crypto');
var fs = require('fs');
var app = express.Router();

function SqlFun() {
	this.connection = mysql.createConnection({
		host: 'localhost',
		user: 'root',
		password: '123456',
		database: 'woyao_tv'
	});
	//防止xss
	this.DefenseXSS = function(xss) {
		return xss.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;").replace(/&/g, "&amp;")
	}
	//网站配置文件地址
	this.SiteConfig = "routes/config/siteconfig.json";
	//友情链接配置文件地址
	this.linkConfig = "routes/config/linkconfig.json";
	//读取配置文件
	this.GetConfig = function(file) {
		return fs.readFileSync(file).toString();
	}
	//写配置文件
	this.SetConfig = function(file, data) {
		fs.writeFile(file, data, function(err) {
			if(err) {
				console.log(err)
				return false;
			}
			return true;
		});
	}
	//配置文件内容
	this.GetStringConfig = JSON.parse(this.GetConfig(this.SiteConfig));

	//加密:encryption  解密:Decrypt
	this.Toolaes = function(data, type) {
		/* 加密方式     */
		var algorithm = 'aes-256-cbc';
		/* 加密key */
		var key = '78541561566';
		// 将结果返回
		if(type == "encryption") {
			var cipher = crypto.createCipher(algorithm, key);
			var crypted = cipher.update(data, 'utf8', 'hex');
			crypted += cipher.final('hex');
			return crypted;
		} else if(type == "Decrypt") {
			var decipher = crypto.createDecipher(algorithm, key);
			var dec = decipher.update(data, 'hex', 'utf8');
			dec += decipher.final('utf8');
			return dec;
		}

	};
	//md5加密
	this.Toolmd5 = function(text) {
		var md5 = crypto.createHash('md5');
		return md5.update(text).digest('hex');
	};
	//获取当前时间戳
	this.timestamp = function() {
		return(new Date()).valueOf();
	}
	//获取当前年月日
	this.yymmdd = function() {
		var nowDate = new Date();
		var year = nowDate.getFullYear();
		var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) :
			nowDate.getMonth() + 1;
		var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
			.getDate();
		var dateStr = year + month + day;
		return dateStr;
	}
	//文件名加密
	this.filename = function(len) {
		var len = len || 32;
		var chars = 'abcdef0123456789'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
		var maxpos = chars.length;
		var str = '';

		for(var i = 0; i < len; i++) {
			str += chars.charAt(Math.floor(Math.random() * maxpos))
		}
		return str;
	}
	//判断是否是图片文件
	this.checkImgType = function(str) {
		var strRegex = "(.jpg|.png|.gif|.jpeg)$"; //用于验证图片扩展名的正则表达式
		var re = new RegExp(strRegex, "i");
		if(re.test(str.toLowerCase())) {
			return true;
		} else {
			return false;
		}
	}
	//判断是否是视频文件
	this.checkImgType = function(str) {
		var strRegex = "(.jpg|.png|.gif|.jpeg)$"; //用于验证图片扩展名的正则表达式
		var re = new RegExp(strRegex, "i");
		if(re.test(str.toLowerCase())) {
			return true;
		} else {
			return false;
		}
	}
	//判断图片是否大于3M
	this.ImageSize = function(size, maxsize) {
		if(size / 1024 / 1024 > maxsize) {
			return false;
		}
		return true;
	}
	//获取文件后缀
	this.upfilename = function(name) {
		var upFileName = name;
		var index1 = upFileName.lastIndexOf(".");
		var index2 = upFileName.length;
		var suffix = upFileName.substring(index1, index2)
		return suffix;
	}
}
module.exports = SqlFun;